package com.njworkorder.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njworkorder.Entity.SysTemplate;
import com.njworkorder.Service.SysTemplateService;
import com.njworkorder.Utils.FileOperation;
import com.njworkorder.Utils.Result;
import com.njworkorder.Utils.ResultUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.io.FileNotFoundException;

@RestController
@RequestMapping("/sysTemplate")
@Tag(name = "系统模板文件管理", description = "系统模板文件管理相关接口")
public class SysTemplateController {

    private final SysTemplateService sysTemplateService;
    public SysTemplateController(SysTemplateService sysTemplateService) {
        this.sysTemplateService = sysTemplateService;
    }

    @Operation(summary = "添加模板信息")
    @GetMapping("/addInfo")
    public Result<String> addInfo(@Schema(description = "模板名称") @RequestParam("templateName") String templateName,
                                  @Schema(description = "下载路径") @RequestParam("downloadPath") String downloadPath,
                                  @Schema(description = "菜单名称") @RequestParam(value = "menuName",required = false) String menuName,
                                  @Schema(description = "模板类型") @RequestParam("templateType") String templateType,
                                  @Schema(description = "备注") @RequestParam(value = "remark",required = false) String remark) {

        SysTemplate sysTemplate = new SysTemplate();
        sysTemplate.setTemplateName(templateName);
        sysTemplate.setDownloadPath(downloadPath);
        sysTemplate.setMenuName(menuName);
        sysTemplate.setTemplateType(templateType);
        sysTemplate.setRemark(remark);
        return sysTemplateService.save(sysTemplate) ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败！");
    }

    @Operation(summary = "根据id删除模板信息")
    @GetMapping("/deleteInfoById")
    public Result<String> deleteInfoById(@RequestParam("id") String id) throws FileNotFoundException {
        SysTemplate byId = sysTemplateService.getById(id);
        boolean b = sysTemplateService.removeById(id);
        if(b){
            boolean b1 = FileOperation.deleteFile(byId.getDownloadPath());
            return ResultUtil.success("删除成功");
        }
        return ResultUtil.fail("删除失败！");
    }

    @Operation(summary = "根据id更新模板信息")
    @GetMapping("/updateInfoById/{id}")
    public Result<String> updateInfoById(@Schema(description = "模板名称") @RequestParam("templateName") String templateName,
                                         @Schema(description = "下载路径") @RequestParam("downloadPath") String downloadPath,
                                         @Schema(description = "菜单名称") @RequestParam("menuName") String menuName,
                                         @PathVariable("id") String id){
        LambdaUpdateWrapper<SysTemplate> wrapper = new LambdaUpdateWrapper<SysTemplate>()
                .set(StringUtils.isNotEmpty(templateName),SysTemplate::getTemplateName, templateName)
                .set(StringUtils.isNotEmpty(downloadPath),SysTemplate::getDownloadPath, downloadPath)
                .set(StringUtils.isNotEmpty(menuName),SysTemplate::getMenuName, menuName)
                .eq(SysTemplate::getId, id);
        return sysTemplateService.update(wrapper) ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败！");
    }

    @Operation(summary = "根据id查询模板信息")
    @GetMapping("getInfoById")
    public Result<SysTemplate> getInfoById(@RequestParam("id") String id){
        SysTemplate byId = sysTemplateService.getById(id);
        return ResultUtil.success(byId);
    }

    @Operation(summary = "获取模板列表信息")
    @GetMapping("/getList")
    public Result<Page<SysTemplate>> getList(@RequestParam("pageIndex") int pageIndex,
                                             @RequestParam("pageSize") int pageSize,
                                             @Schema(description = "模板名称") @RequestParam(value = "templateName",required = false) String templateName,
                                             @Schema(description = "菜单名称") @RequestParam(value = "menuName",required = false) String menuName){
        LambdaQueryWrapper<SysTemplate> wrapper = new LambdaQueryWrapper<SysTemplate>()
                .apply("1=1")
                .eq(StringUtils.isNotEmpty(templateName),SysTemplate::getTemplateName, templateName)
                .eq(StringUtils.isNotEmpty(menuName),SysTemplate::getMenuName, menuName);

        Page<SysTemplate> page = sysTemplateService.page(new Page<>(pageIndex, pageSize), wrapper);
        return ResultUtil.success(page);
    }

}
